/**
* 支付管理 支付配置信息
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>支付配置信息</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.tnumber" placeholder="输入终端号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="opRight.includes('save')" size="small" type="primary" icon="el-icon-plus"
                   @click="handleEdit()">添加
        </el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
              element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="tnumber" label="终端号" width="200" align="center">
      </el-table-column>
      <el-table-column sortable prop="mode" label="支付方式" width="100" align="center">
      </el-table-column>
      <el-table-column sortable prop="name" label="显示名称" width="200" align="center">
      </el-table-column>
      <el-table-column sortable prop="status" label="状态" width="100" align="center">
      </el-table-column>
      <el-table-column sortable prop="editTime" label="修改时间" width="200" align="center">
        <template slot-scope="scope">
          <div>{{scope.row.editTime|timestampToTime}}</div>
          <!--          </el-switch>-->
        </template>
      </el-table-column>
      <el-table-column sortable prop="editUser" label="修改人" width="150" align="center">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button v-if="opRight.includes('save')" size="mini" @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button v-if="opRight.includes('delete')" size="mini" type="danger"
                     @click="deleteUser(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="终端号" prop="tnumber">
          <el-input size="small" v-model="editForm.tnumber" auto-complete="off" placeholder="请输入终端号"></el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="mode">
          <el-select size="small" v-model="editForm.mode" placeholder="请选择" class="userRole">
            <el-option v-for="type in mode" :label="type.name" :value="type.id" :key="type.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置序号" prop="configId">
          <el-input size="small" v-model="editForm.configId" auto-complete="off" placeholder="请输入配置序号"></el-input>
        </el-form-item>
        <!--        <el-form-item label="显示名称" prop="name">-->
        <!--          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入显示名称"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-select size="small" v-model="editForm.status" placeholder="请选择" class="userRole">
            <el-option label="正常" value="T"></el-option>
            <el-option label="禁用" value="N"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button v-if="opRight.includes('save')" size="small" type="primary" :loading="loading" class="title"
                   @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    MachineConfigList,
    MachineConfigSave,
    MachineConfigDelete,
    ModeList,
  } from '../../api/payMG'
  import Pagination from '../../components/Pagination'
  import {UserOpRight} from "../../api/userMG";
  import {lert} from "../../api/axiosFun";

  export default {
    data() {
      return {
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        editFormVisible: false, //控制编辑页面显示与隐藏
        title: '添加',
        opRight: [],
        login: {
          code: 'cnfInfo',
          token: localStorage.getItem('userdata')
        },
        mode: [],
        editForm: {
          issave: false,
          id: '',
          tnumber: '',
          mode: '',
          configId: '',
          name: '',
          status: '',
          token: localStorage.getItem('username')
        },
        // rules表单验证
        rules: {
          tnumber: [
            {required: true, message: '请输入终端号', trigger: 'blur'}
          ],
          mode: [
            {required: true, message: '请选择支付方式', trigger: 'blur'}
          ],
          configId: [
            {required: true, message: '请输入配置序号', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入显示名称', trigger: 'blur'}
          ],
          status: [{required: true, message: '请选择状态', trigger: 'blur'}]
        },
        formInline: {
          page: 1,
          limit: 10,
          tnumber: '',
          varLable: '',
          varName: '',
          token: localStorage.getItem('username')
        },
        // 删除部门
        seletedata: {
          ids: '',
          token: localStorage.getItem('logintoken')
        },
        userparm: [], //搜索权限
        listData: [], //用户数据
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },
    /**
     * 数据发生改变
     */

    /**
     * 创建完毕
     */
    created() {
      this.getMode()
      this.getdata(this.formInline)
    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      //获取支付配置信息
      getMode() {
        ModeList()
          .then(res => {
            lert(res);
            this.mode = JSON.parse(res.data)
          });
      },
      // 获取公司列表
      getdata(parameter) {
        this.loading = true
        //获取当前页面的操作权限(opRight)
        UserOpRight(this.login)
          .then(res => {
            this.opRight = JSON.parse(res.data);
          })
        /***
         * 调用接口，注释上面模拟数据 取消下面注释
         */
        MachineConfigList(parameter)
          .then(res => {
            this.loading = false
            if (res.success == false) {
              this.$message({
                type: 'error',
                message: res.msg
              })
            } else {
              this.loading = false
              var result = JSON.parse(res.data);
              this.listData = result.records
              // 分页赋值
              this.pageparm.currentPage = result.current
              this.pageparm.pageSize = result.size
              this.pageparm.total = result.total
              this.$message.success(res.msg)
            }
          })
          .catch(err => {
            this.loading = false
            this.$message.error('菜单加载失败，请稍后再试！')
          })
      },

      // 模拟数据
      // let res = {
      //   code: 0,
      //   msg: null,
      //   count: 5,
      //   data: [
      //     {
      //       addUser: null,
      //       editUser: null,
      //       addTime: null,
      //       editTime: 1524046759000,
      //       id: 1,
      //       deptId: 1,
      //       tnumber: '564565656666',
      //       mode: 3,
      //       status: 'T',
      //       configId: 63,
      //       name: '微信',
      //       posNo: '098'
      //     }

      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      // 搜索事件
      search() {
        this.getdata(this.formInline)
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '修改'
          this.editForm.issave = false
          this.editForm.id = row.id
          this.editForm.tnumber = row.tnumber
          this.editForm.configId = row.configId
          this.editForm.mode = row.mode
          this.editForm.name = row.name
          this.editForm.status = row.status

        } else {
          this.editForm.issave = true
          this.title = '添加'
          this.editForm.id = ''
          this.editForm.tnumber = ''
          this.editForm.mode = ''
          this.editForm.configId = ''
          this.editForm.name = ''
          this.editForm.status = ''

        }
      },
      // 编辑、增加页面保存方法
      submitForm(editData) {
        this.$refs[editData].validate(valid => {
          if (valid) {
            MachineConfigSave(this.editForm)
              .then(res => {
                lert(res);
                this.editFormVisible = false
                this.loading = false
                if (res.success) {
                  this.getdata(this.formInline)
                  this.$message({
                    type: 'success',
                    message: '支付配置信息保存成功！'
                  })
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.editFormVisible = false
                this.loading = false
                this.$message.error('支付配置信息保存失败，请稍后再试！')
              })
          } else {
            return false
          }
        })
      },
      // 删除支付配置信息
      deleteUser(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            MachineConfigDelete(row.id)
              .then(res => {
                lert(res);
                if (res.success) {
                  this.$message({
                    type: 'success',
                    message: '支付配置信息已删除!'
                  })
                  this.getdata(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('支付配置信息删除失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      },
      // 关闭编辑、增加弹出框
      closeDialog() {
        this.editFormVisible = false
      }
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
  }

  .userRole {
    width: 100%;
  }
</style>


